home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / HDX_BACK / HDX504 / HDX.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  58.7 KB  |  2,370 lines

  1. /* hdx.c */
  2.  
  3. /*
  4.  * Atari Hard Disk Installation Utility
  5.  * Copyright 1988 Atari Corp.
  6.  *
  7.  * Associated files
  8.  *    hdx.rsc        resource file
  9.  *    wincap        hard disk database (text file)
  10.  *
  11.  *    hdx.h        object tree definitions
  12.  *    defs.h        constant definitions
  13.  *    part.h        ST structure definitions
  14.  *    ipart.h        IBM structure definitions
  15.  *
  16.  *    hdx.c        top level, user interface (this file)
  17.  *    epart.c        edit partition sizes
  18.  *    fmt.c        disk formatting
  19.  *    part.c        partition reading/writing
  20.  *    sect.c        sector reading, writing, zeroing
  21.  *    string.c    string functions (matching, concat, ...)
  22.  *    assist.c    markbad(), zero()
  23.  *    wincap.c    hard disk parameter / partition size database
  24.  *    st.c        random ST functions (delay, reboot, ...)
  25.  *
  26.  *----
  27.  * 11-May-1988    ml.    Cleaned up the memory management in the program
  28.  *            (ie. for all files).  Memory chunks which are for
  29.  *            sure will be < 32k is allocated using malloc(),
  30.  *            whereas chunks >= 32k is allocated using Malloc().
  31.  *            When using malloc(), you will get the 'Stack Over-
  32.  *            flow message if you are in Supervisor mode and 
  33.  *            you have your own supervisor stack.  To get around
  34.  *            this, have to use mymalloc() (in mymalloc.s).
  35.  * 15-Mar-1988    ml.    Changed interface to Markbad.
  36.  * 11-Jan-1988    ml.    Modified dialogue boxes.
  37.  * 07-Dec-1987    ml.    Started to add in concept of Bad Sector List.
  38.  * ??-Oct-1987  ml.    Partition and Disk type menu now has 15 entries 
  39.  *            instead of 16.
  40.  * 30-Sep-1987    ml.    Inherited 'this' from Landon Dyer.
  41.  * 24-Mar-1986 lmd    Released to software test.
  42.  * 15-Mar-1986 lmd    Hacked up from earlier version by Jim Tittsler.
  43.  * 8-Nov-1988  jye. change and add some codes so that can be used for extended
  44.  *                    and big partition.
  45.  * 13-Jun-1989 jye. Change and add some codes so that HDX can be deal with
  46.  *                    acsi and scsi drives.
  47.  * 22-Jan-1990 jye. Change the HDX so that we don't have to put a informations
  48.  *                    of a drive into the Wincap file.
  49.  * 13-Mar-1990 jye. change the HDX to a genetic one so that the user don't
  50.  *                    need know what kind of drive.
  51.  * 20-Jul-1990 jye. change the interface about the unit select. In the new 
  52.  *                    interface, user tells the type of drive is acsi or scsi,
  53.  *                    then selects unit.
  54.  * 01-Aug-1990 jye. Change hard disk size request from mdsence to readcap.
  55.  * 25-Jul-1991 jye. Change the Hdx so that can be used for IDE-AT drive.
  56.  * 11-Nov-1991 jye. change the hdx so that can got the hard disk size from
  57.  *                    page 0, or 3, or 4, or from the wincap. For the SCSI drive,
  58.  *                    getting the disk size from wincap or readcap.
  59.  * 7-Jul-1992  jye. Change the hdx so that can be used for the sparrow scsi.
  60.  * Feb-3-1993  jye. Fixed a bug which is when format a hard disk with no wincap
  61.  *                    file, then the HDX will bomb out. Now, you can still format
  62.  *                    the hard disk without the wincap, because the wincap file
  63.  *                    is used when HDX can't get the hard disk's informations.
  64.  * Apr-9-1993  jye.    To fixed a problem for the Conner CP2088 drive, which HDX
  65.  *                    can't Markbad it in Format, because this kind of drive has
  66.  *                    the different values of head, cylinder, and spt between the
  67.  *                    physical and logical parameters, so I added a inqury() call
  68.  *                    right after the 'fmtunt()' in 'dodiform()' to up date the
  69.  *                    table, because 'fmtunt()' changes the physical parameters 
  70.  *                    to the logical parameters.
  71.  */
  72.  
  73. #include "obdefs.h"
  74. #include "gemdefs.h"
  75. #include "osbind.h"
  76. #include "mydefs.h"
  77. #include "part.h"
  78. #include "bsl.h"
  79. #include "hdx.h"
  80. #include "ipart.h"
  81. #include "addr.h"
  82. #include "myerror.h"
  83.  
  84. #define MFM 17
  85.  
  86. extern char sbuf[];
  87. extern int rebootp;
  88. extern long gbslsiz();
  89. extern long get3bytes();
  90. extern long get4bytes();
  91. extern long bslsiz;
  92. extern BYTE *bsl;
  93. extern int uplim;            /* the number of partitions */
  94. extern long sptrk;
  95. extern long spcyl;
  96. extern int ibmpart;
  97. extern int yesscan;
  98. extern long disksiz;
  99. extern long ratio;
  100. extern int typedev;
  101. extern int typedrv;
  102. extern int prevnpart;
  103. extern int atexst;        /* set for AT drive exist */
  104.  
  105. /* Globals */
  106. int rebootp = 0;    /* 1: must reboot (not return to desktop) */
  107. int tformat;            /* TRUE: just formatted disk */
  108. int running;        /* 1: continue evnt_multi() loop */
  109. char sbuf[512];        /* error message buffer */
  110. long extsiz;        /* the size of extened partition */
  111. long ostack;        /* old stack pointer */
  112. int toibm;            /* the flag of partition to ibm format */
  113. int ibm2st;            /* the flag for IBM partition to ST */
  114. long bps;            /* bytes per sector */
  115. int npart;            /* number of partition */
  116. int ext;            /* the index of extended partition */
  117. int extend;            /* the index of end extended partition */
  118. int showmany;        /* the flag that show the too much device alert box */
  119. char ttscsi;        /* SCSI hard disk drive */
  120. char spscsixst;        /* set for sparrow scsi drive exist */
  121. int noacsi;            /* set for no ACSI dirve in the system */
  122. int needscan;        /* TRUE: if info in the LOGMAP update */
  123. int noinfo=1;            /* 1: no informations in the wincap */
  124. int athead;            /* the # of data heads on the AT drive */
  125. int atcyl;            /* the # of cylinders on the AT drive */
  126. int atspt;            /* the # of sectors per track on the AT drive */
  127. int ok2draw;        /* go ahead to draw the change part of dialog box */
  128. int ndevs;            /* the # of devices */
  129. char *drvid[] ={    /* for the id of the drives */
  130.     "123456789012345678901234567",
  131.     "123456789012345678901234567",
  132.     "123456789012345678901234567",
  133.     "123456789012345678901234567",
  134.     "123456789012345678901234567",
  135.     "123456789012345678901234567",
  136.     "123456789012345678901234567",
  137.     "123456789012345678901234567",
  138.     "123456789012345678901234567",
  139.     "123456789012345678901234567",
  140.     "123456789012345678901234567",
  141.     "123456789012345678901234567",
  142.     "123456789012345678901234567",
  143.     "123456789012345678901234567",
  144.     "123456789012345678901234567",
  145.     "123456789012345678901234567",
  146.     "123456789012345678901234567",
  147.     "Identification unavaliable",
  148.     "Not responding",
  149.     "ACSI 0",        /* index is 19 */
  150.     "ACSI 1",
  151.     "ACSI 2",
  152.     "ACSI 3",
  153.     "ACSI 4",
  154.     "ACSI 5",
  155.     "ACSI 6",
  156.     "ACSI 7",
  157.     "SCSI 0",        /* index is 27 */
  158.     "SCSI 1",
  159.     "SCSI 2",
  160.     "SCSI 3",
  161.     "SCSI 4",
  162.     "SCSI 5",
  163.     "SCSI 6",
  164.     "SCSI 7",
  165.     "IDE 0",            /* index is 35 */
  166.     " "
  167. };
  168.  
  169. DEVSET devid[17];
  170.  
  171. /*  Logical-to-dev+partition mapping table. */
  172. extern int nlogdevs;        /* #logical devs found */
  173. extern LOGMAP logmap[];        /* logical dev map */
  174. extern int livedevs[];        /* live physical dev flag */
  175. extern int idevs[];            /* the devise have a id */
  176. extern char cachexst;        /* 0: no cache. 1: with cache */
  177.  
  178. /* Partition related variables */
  179. long mxpsiz = MAXPSIZ;
  180.  
  181. /* AES (windows and messages) related variables */
  182. int gl_hchar;        /* height of system font (pixels) */
  183. int gl_wchar;        /* width of system font (pixels) */
  184. int gl_wbox;        /* width of box able to hold system font */
  185. int gl_hbox;        /* height of box able to hold system font */
  186.  
  187. int phys_handle;    /* physical workstation handle */
  188. int handle;        /* virtual workstation handle */
  189. int wi_handle;        /* window handle */
  190.  
  191. int formw, formh, sx, sy, lx, ly;    /* dialogue box dimensions */
  192. int xdesk, ydesk, hdesk, wdesk;        /* window X, Y, width, height */
  193. int xwork, ywork, hwork, wwork;        /* desktop and work areas */
  194.  
  195. int msgbuff[8];        /* event message buffer */
  196. int keycode;        /* keycode returned by event-keyboard */
  197. int mx, my;        /* mouse x and y pos. */
  198. int butdown;        /* button state tested for, UP/DOWN */
  199. int ret;        /* dummy return variable */
  200. int pnf;        /* 1: partition or format; 0: zero or markbad */
  201. int hidden;        /* current state of cursor */
  202. int contrl[12];
  203. int intin[128];
  204. int ptsin[128];
  205. int intout[128];
  206. int ptsout[128];    /* storage wasted for idiotic bindings */
  207. int work_in[11];    /* Input to GSX parameter array */
  208. int work_out[57];    /* Output from GSX parameter array */
  209. int pxyarray[10];    /* input point array */
  210. int blitxst;        /* the flag for check the BLiTTER */
  211.  
  212. /*
  213.  * Top level;
  214.  * we get control from the desktop.
  215.  */
  216. main()
  217. {
  218.     pnf = 0;        /* set the flag to it isn't partition yet */
  219.     appl_init();
  220.     phys_handle=graf_handle(&gl_wchar, &gl_hchar, &gl_wbox, &gl_hbox);
  221.     wind_get(0, WF_WORKXYWH, &xdesk, &ydesk, &wdesk, &hdesk);
  222.     open_vwork();
  223.     wi_handle=wind_create(0x0040&0x0080, xdesk, ydesk, wdesk, hdesk);
  224.  
  225.     hidden = FALSE;
  226.     butdown = TRUE;
  227.  
  228.     /* doing a checking see if the cache in the system */
  229.     cachexst = (char) chkcache();
  230.  
  231.     /* check the existence of the BLiTTER */
  232.     blitxst = chkblit();
  233.  
  234.     if (!rsrc_load(RESOURCEFILE)) {
  235.     errs("[2][|", RESOURCEFILE, "][ EXIT ]");
  236.     goto punt;
  237.     }
  238.  
  239.     
  240.     /* Get all addresses of dialogues from resource file */
  241.     if (getalladdr() != OK) {
  242.     errs("[2][|", RESOURCEFILE, "][ EXIT ]");
  243.     goto punt;
  244.     }
  245.  
  246.  
  247.     /*
  248.      * Get maximum partition size from
  249.      * wincap "@@" entry.
  250.      */
  251.     /*
  252.     if (wgetent("Parameters", "@@") == OK) {
  253.     if (wgetnum("ms", &mxpsiz) != OK)
  254.         mxpsiz = MAXPSIZ;
  255.     } else {
  256.         goto punt;
  257.     }
  258.     */
  259.  
  260.     needscan = TRUE;
  261.  
  262. redomul:
  263.     ARROW_MOUSE;
  264.  
  265.     /* display menu bar */
  266.     menu_bar(menuobj, 1);
  267.  
  268.     running = TRUE;
  269.     while (running) {
  270.         domulti();
  271.     }
  272.  
  273.     /*
  274.      * If nothing has been done to the hard disks
  275.      * then just get out, back to the desktop.
  276.      * Otherwise reboot the system.
  277.      */
  278.     menu_bar(menuobj, 0);        /* erase menu bar */
  279.  
  280. punt:
  281.     /*
  282.      * If we have to reboot,
  283.      * tell the user and then do it.
  284.      *
  285.      */
  286.     if (rebootp) {
  287.         if (form_alert(2, autoboot) == 1)    {
  288.             reboot();
  289.         } else {
  290.             goto redomul;
  291.         }
  292.     }
  293.  
  294.     wind_delete(wi_handle);
  295.     v_clsvwk(handle);
  296.     appl_exit();
  297. }
  298.  
  299.  
  300. /*
  301.  * Get a single event, process it, and return.
  302.  *
  303.  */
  304. domulti(){
  305.     int event;
  306.     
  307.     event = evnt_multi(MU_MESAG,
  308.             1,1,butdown,
  309.             0,0,0,0,0,
  310.             0,0,0,0,0,
  311.             msgbuff,0,0,&mx,&my,&ret,&ret,&keycode,&ret);
  312.  
  313.     if (event & MU_MESAG) {
  314.         wind_update(TRUE);
  315.     switch (msgbuff[0]) {
  316.         case WM_REDRAW:
  317.         do_redraw(msgbuff[4],msgbuff[5],msgbuff[6],msgbuff[7]);
  318.         break;
  319.  
  320.         case MN_SELECTED:
  321.             BEE_MOUSE;
  322.         switch(msgbuff[3]) {
  323.             case MNDISK:
  324.             switch (msgbuff[4]) {
  325.                 case DIFORM:
  326.                     if ((needscan) && (rescan(0,1) == ERROR))    {
  327.                         break;    /* don't report medium changed */
  328.                     }
  329.                     tformat = TRUE;
  330.                     needscan = FALSE;
  331.                     dodiform();
  332.                     tformat = FALSE;
  333.                     break;
  334.                 case DIPART:
  335.                     if ((needscan)&&(rescan(0,2) == ERROR))    {
  336.                         break;    /* don't report medium changed */
  337.                     }
  338.                     needscan = FALSE;
  339.                     dodipart(-1, NULL, NULL);
  340.                     break;
  341.                 case DIZERO:
  342.                     if (pnf)    {
  343.                         err(needboot);
  344.                     } else {
  345.                         if ((needscan)&&(rescan(0,0) == ERROR))    {
  346.                             break;    /* don't report medium changed */
  347.                         }
  348.                         needscan = FALSE;
  349.                         dodizero();
  350.                     }
  351.                     break;
  352.                 case DIMARK:
  353.                     if (pnf)    {
  354.                         err(needboot);
  355.                     } else {
  356.                         if ((needscan)&&(rescan(0,0) == ERROR))    {
  357.                             break;    /* don't report medium changed */
  358.                         }
  359.                         needscan = FALSE;
  360.                         dodimark();
  361.                     }
  362.                     break;
  363.                 /*
  364.                 case DISHIP:
  365.                     if ((needscan)&&(rescan(0,1) == ERROR))    {
  366.                         break; don't report medium changed
  367.                     }
  368.                     needscan = FALSE;
  369.                     dodiship();
  370.                     break;
  371.                 */
  372.                 default:        break;
  373.             }
  374.             break;
  375.  
  376.             case MNFILE:
  377.             switch (msgbuff[4]) {
  378.                 case FIQUIT:
  379.                 running = 0;
  380.                 break;
  381.  
  382.                 default:
  383.                 break;
  384.             }
  385.             break;
  386.             
  387.             case MNDESK:
  388.             if(msgbuff[4] == DEABOUT) {
  389.                 strcpy(abtdial[ABVERSN].ob_spec, "Version 5.04");
  390.                  abtdial[ABOK].ob_state = NORMAL;
  391.                 execform(abtdial);
  392.             }
  393.             break;        /* "cannot happen" */
  394.         }
  395.  
  396.         menu_tnormal(menuobj, msgbuff[3], 1);    /* back to normal */
  397.             ARROW_MOUSE;
  398.         break;
  399.         
  400.         case WM_NEWTOP:
  401.         case WM_TOPPED:
  402.         wind_set(wi_handle, WF_TOP, 0, 0, 0, 0);
  403.         break;
  404.  
  405.         case WM_CLOSED:
  406.         running = FALSE;
  407.         break;
  408.  
  409.         default:
  410.         break;
  411.     }
  412.     wind_update(FALSE);
  413.     }
  414. }
  415.  
  416.  
  417. /*
  418.  * Default partition name (no "pt" entry).
  419.  */
  420. #define    DEF_PARTNAME    "4-6-10"
  421.  
  422.  
  423. /*
  424.  * Map from button in format dial.
  425.  */
  426. int pfmt[] = {
  427.     PFMT0, PFMT1, PFMT2, PFMT3,
  428.     PFMT4, PFMT5, PFMT6, PFMT7,
  429.     PFMT8, PFMT9, PFMT10, PFMT11,
  430.     PFMT12, PFMT13, PFMT14, PFMT15
  431. };
  432.  
  433.  
  434. /*
  435.  * Handle [FORMAT] item.
  436.  *
  437.  */
  438. dodiform()
  439. {
  440.   extern char bootstop;
  441.   extern char bootend;
  442.   int dev, v, w, i, br;
  443.   int modesel;            /* flag for mode select */
  444.   long cnt, hdsiz;
  445.   char *s, *d, *wgetstr();
  446.   char buf[10], bs[512], sendata[32];
  447.   char pnam[128];
  448.   char *seldev = "ACSI unit 0", *id = "XXXXX";
  449.   char *seldv =  "IDE  unit 0";
  450.   HINFO hinfo;
  451.   char devnames[NAMSIZ];    /* device type name buffer */
  452.   long nbad;
  453.   extern long gbslsiz(), nument(), dsmarkbad();
  454.   long pattern, temp, cyl;
  455.   long longrandom();
  456.   char pr_id[10];    /* partition scheme id */
  457.   int mask = 0x0001;
  458.   int set, scsidrv, bsiz, other = 0;
  459.   int page=4, spt, ret;
  460.   int head=0, domark=YESMK;
  461.   
  462.   /*
  463.    * Throw up generic formatting/partition warning,
  464.    * then get physical dev they want to clobber.
  465.    */
  466.   yesscan = 0;
  467.   noinfo = 1;
  468.   for (i = 0; i < NAMSIZ; i++)
  469.       devnames[i] = "\0";
  470.   fwarning[FWARNCN].ob_state = NORMAL;
  471.   fwarning[FWARNOK].ob_state = NORMAL;
  472.   if (execform(fwarning) != FWARNOK) return BAILOUT;
  473.  
  474.   if ((dev = gphysdev()) < 0) {
  475.       return BAILOUT;
  476.   }
  477.   strcpy(id, "mn");
  478.   br = 0;
  479.  
  480.   /* display doing SCSI massage */
  481.  
  482.   inqfmt:
  483.   /* Get all available disk types from wincap 'mn' entries */  
  484.   if (wallents(devnames, id) != OK)        {
  485.       goto stfm;
  486.   }
  487.   if (!*devnames)     {/* no informations */
  488.       goto stfm;
  489.   }
  490.   noinfo = 0;
  491.   /* Shove format name text into buttons */
  492.   for (i = 0, s = devnames; i < 14 && *s; ++i) {
  493.       dsknames[pfmt[i]].ob_type = G_BUTTON;    /* button */
  494.       dsknames[pfmt[i]].ob_spec = (long)s;
  495.       dsknames[pfmt[i]].ob_state = NORMAL;
  496.       dsknames[pfmt[i]].ob_flags = SELECTABLE | RBUTTON;
  497.       while (*s++)
  498.     ;
  499.   }
  500.   other = i;                            /* the other button */
  501.   dsknames[pfmt[i]].ob_type = G_BUTTON;    /* button */
  502.   dsknames[pfmt[i]].ob_spec = "OTHER";
  503.   dsknames[pfmt[i]].ob_state = NORMAL;
  504.   dsknames[pfmt[i]].ob_flags = SELECTABLE | RBUTTON;
  505.   i++;
  506.  
  507.   /* rest of buttons are invisible and untouchable */
  508.   for (; i < 16; ++i) {
  509.       dsknames[pfmt[i]].ob_type = G_IBOX;    /* invisible box */
  510.       dsknames[pfmt[i]].ob_spec = 0;        /* no thickness */
  511.       dsknames[pfmt[i]].ob_state = DISABLED;    /* nobody home */
  512.       dsknames[pfmt[i]].ob_flags = NONE;        /* disabled */
  513.   }
  514.   
  515.   /* clean up rest of the form and throw it up */
  516.   dsknames[PFOK].ob_state = NORMAL;
  517.   dsknames[PFCN].ob_state = NORMAL;
  518.   if (execform(dsknames) != PFOK)
  519.     return BAILOUT;
  520.   
  521.   /* search for format they picked */
  522.   for (i = 0; i < 18; ++i)
  523.     if (dsknames[pfmt[i]].ob_state & SELECTED)
  524.       break;
  525.   if (i >= 18) {        /* nothing picked */
  526.       return BAILOUT;
  527.   } else if (other == i)    {  /* the OTHER button was selected */
  528.         noinfo = 1;
  529.       goto stfm;
  530.   }
  531.   
  532.     if ((!br) && (dev < 8))        {
  533.           if (wgetent(dsknames[pfmt[i]].ob_spec, "mn") == ERROR)    {
  534.               strcpy(id, "mn");
  535.             noinfo = 1;
  536.             goto stfm;
  537.             /*
  538.             nofmt[NOSCHFMT].ob_spec = dsknames[pfmt[i]].ob_spec;
  539.             nofmt[NOSCHFMT].ob_state = NORMAL;
  540.             execform(nofmt, 0);
  541.             return ERROR;
  542.             */
  543.         }
  544.         if ((s = wgetstr("br")) != NULL)    {
  545.             strcpy(id, s);
  546.             br = 1;            /* processing the branch */
  547.             goto inqfmt;    /* start over */
  548.         }
  549.     }
  550.  
  551. stfm:
  552.     modesel = 1;
  553.   if(gfparm(dev, &noinfo, &modesel, &hinfo, dsknames[pfmt[i]].ob_spec,id)!= 0) {
  554.       return ERROR;
  555.   }
  556.   
  557.   /* get data pattern to test the disk */
  558.   if (wgetnum("dp", &pattern) != OK) {
  559.       pattern = longrandom();  /* can't find pattern from wincap, make one */
  560.   } else {
  561.       temp = pattern;
  562.       pattern <<= 16;    /* shift pattern to hi word */
  563.       pattern |= temp;
  564.   }
  565.  
  566.   if (dev > 15)    { /* IDE-AT drive */
  567.       br = dev - 16;
  568.   } else {            /* it is a scsi or acsi drive */
  569.       br = (dev > 7) ? (dev-8) : (dev);    /* minus 8 for the scsi drive number */
  570.   }
  571.  
  572.   /* asking the user do the marking bad or not */
  573.   mkornot[YESMK].ob_state = NORMAL;
  574.   mkornot[NOTMK].ob_state = NORMAL;
  575.   domark = execform(mkornot);
  576.  
  577.   /*
  578.    * One last chance to bail out.
  579.    */
  580.   *seldev = 'A';
  581.   if (dev > 15)    { /* it is a IDE-AT drive */
  582.       seldev = seldv;
  583.   } else if (dev > 7)    { /* it is a SCSI drive */
  584.           *seldev = 'S';
  585.   }
  586.   *(seldev + 10) = br + '0';
  587.   (fmtfnl[FUNIT].ob_spec)->te_ptext = seldev;
  588.   fmtfnl[FMTYES].ob_state = NORMAL;
  589.   fmtfnl[FMTNO].ob_state = NORMAL;
  590.   if (execform(fmtfnl) != FMTYES) return BAILOUT;
  591.  
  592.   /* For REAL !! */  
  593.   dsplymsg(fmtmsg);
  594.  
  595.   bsl = 0L;
  596.   
  597.   if ((dev > 15) && (!(athead*atcyl*atspt)))    {
  598.       formaterr(dev);
  599.     ret = ERROR;
  600.     goto formend;
  601.   }
  602.   /* Get size of Bad Sector List */
  603.   if ((bslsiz = gbslsiz(dev)) > 0L) {
  604.       /* Allocate memory for existing BSL */
  605.       if ((bsl = (BYTE *)mymalloc((int)bslsiz*512)) <= 0) {
  606.           ret = err(nomemory);
  607.           goto formend;
  608.       }
  609.       
  610.       /* Read in BSL */
  611.       if ((ret = rdbsl(dev)) != OK) {
  612.           /* Create a new BSL if current one is unusable */
  613.           if (creabsl(dev, NEW, 0L) != OK) {
  614.               ret = ERROR;
  615.               goto formend;
  616.           }
  617.       } else {
  618.             /* Remove USER BSL */
  619.             if (creabsl(dev, EXPAND, nument(VENDOR)) != OK) {
  620.                 ret = ERROR;
  621.                 goto formend;
  622.             }
  623.       }
  624.   } else if (bslsiz == 0L || bslsiz == ERROR) {    /* no bsl or read error */
  625.       if (creabsl(dev, NEW, 0L) != OK) {
  626.           ret = ERROR;
  627.           goto formend;
  628.       }
  629.   } else {    /* bslsiz == MDMERR; medium changed error */
  630.       ret = ERROR;
  631.       goto formend;
  632.   }
  633.   
  634.   /*
  635.    * In supervisor mode
  636.    * set disk format parameters
  637.    * and format the disk.
  638.    */
  639.   ostack = Super(NULL);
  640.   delay();
  641.   if (dev > 15)        { /* do the IDE-AT format */
  642.      if ((ret = fmtunt(dev)) != OK)    {
  643.       delay();
  644.         Super(ostack);
  645.       ret = errcode(dev);
  646.       if (tsterr(ret) != OK)
  647.           formaterr(dev);
  648.       ret = ERROR;
  649.     } else {
  650.         /* Apr-7-93 jye: This is a farmware bug, so have to recall identify()*/
  651.         /*             to update the head, cylinder, and spt                      */
  652.         if (identify(16, bs) == OK)    {
  653.             delay();
  654.             gparmfc(&atcyl, &athead, &atspt, bs); 
  655.         } else {
  656.             ret = ERROR;
  657.             goto formend;
  658.         }
  659.         if (dev == 16)    {/* it is a master IDE-AT */
  660.             hdsiz = athead*atcyl*atspt; 
  661.         } 
  662.           disksiz = hdsiz;
  663.         delay();
  664.           Super(ostack);
  665.     }
  666.     goto formend;
  667.   } 
  668.   set = typedev & (mask << dev);
  669.   scsidrv = typedrv & (mask << dev);
  670.   bsiz = ((set) || (scsidrv)) ? (16) : (22);
  671.   if (!noinfo)    {            /* there are info in the wincap */
  672.     if ((set) || (scsidrv))    {
  673.           v = mdsense(dev, 4, 0, 32, sendata);
  674.           delay();                    /* kludge delay */
  675.           sqms(dev, sendata);
  676.     } else {
  677.           if (modesel) {
  678.               v = ms(dev, &hinfo);    /* Yes, do noprmal mode set */
  679.         }
  680.     }
  681.       /* Find formatted capacity of drive */
  682.       hdsiz = (long)hinfo.hi_cc * (long)hinfo.hi_dhc * (long)hinfo.hi_spt;
  683.   }    else if (dev < 8)    {        /* No, do special mode set */
  684.     for (i = 0; i < 32; i++)
  685.         sendata[i] = 0;
  686.     if ((set) || (scsidrv))    {
  687.           if (mdsense(dev, 0, 0, bsiz, sendata) == OK)        {
  688.             if (hdsiz = get3bytes(sendata+5))    {
  689.                   delay();                    /* kludge delay */
  690.                 sqms(dev, sendata);
  691.                 v = OK;
  692.                 goto kkk;
  693.             }
  694.         }
  695.   redopg:
  696.         for (i = 0; i < 32; i++)
  697.             sendata[i] = 0;
  698.           v = mdsense(dev, page, 0, 32, sendata);
  699.           for (i = 0; i < 32; i++) /* the lenght of sendata */
  700.               if (sendata[i])
  701.                 break;
  702.           if ((i == 32) && (page == 4))    { /* no informations returned */
  703.             page = 3;
  704.             goto redopg;
  705.           } else if (i == 32)    {
  706.             ret = 111;
  707.               delay();                    /* kludge delay */
  708.               Super(ostack);
  709.             goto formend;
  710.         }
  711.         if (page == 4)    {
  712.             if (!(hdsiz = get3bytes(sendata+5)))    {
  713.                 page = 3;
  714.                 /*
  715.                 cyl = get3bytes(sendata+14);
  716.                 head = *(sendata+17);
  717.                 if ((!cyl) || (!head))    {
  718.                     ret = 111;
  719.                       delay();        
  720.                       Super(ostack);
  721.                     goto formend;
  722.                 }
  723.                 */
  724.                 sqms(dev, sendata);
  725.                 goto redopg;
  726.             }
  727.             sqms(dev, sendata);
  728.             goto kkk;
  729.         } 
  730.         if (!(hdsiz = get3bytes(sendata+5)))    {
  731.             /*
  732.             spt = getword(sendata+22);
  733.             hdsiz = cyl*head*spt;
  734.             */
  735.             if (!hdsiz)        {
  736.                 ret = 111;
  737.                   delay();                    /* kludge delay */
  738.                   Super(ostack);
  739.                 goto formend;
  740.             }
  741.         }
  742.         goto kkk;
  743.     } else {    /* it is the acsi drivers */
  744.           hdsiz = (long)hinfo.hi_cc * (long)hinfo.hi_dhc * (long)hinfo.hi_spt;
  745.           delay();                    /* kludge delay */
  746.           v = ms(dev, &hinfo);    /* Yes, do noprmal mode set */
  747.     }
  748.   }    else {
  749.         if ((v = readcap(dev, 0, (long)0, buf)) == OK)    {
  750.             if ((hdsiz = get4bytes(buf)))    {
  751.                 hdsiz += 1;
  752.                 ret = OK;
  753.                 goto kkk;
  754.             } 
  755.         } 
  756.           delay();                    /* kludge delay */
  757.         for (i = 0; i < 32; i++)
  758.             sendata[i] = 0;
  759.           if ((v=mdsense(dev, 0, 0, bsiz, sendata)) == OK)        {
  760.             if (hdsiz = get3bytes(sendata+5))    {
  761.                 ret = OK;
  762.                 goto kkk;
  763.             }
  764.         }
  765.         for (i = 0; i < 32; i++)
  766.             sendata[i] = 0;
  767.           delay();                    /* kludge delay */
  768.           if ((v=mdsense(dev, 4, 0, 32, sendata)) == OK)        {
  769.             if (hdsiz = get3bytes(sendata+5))    {
  770.                 ret = OK;
  771.                 goto kkk;
  772.             }
  773.         }
  774.         for (i = 0; i < 32; i++)
  775.             sendata[i] = 0;
  776.           delay();                    /* kludge delay */
  777.           if ((v=mdsense(dev, 3, 0, 32, sendata)) == OK)        {
  778.             if (hdsiz = get3bytes(sendata+5))    {
  779.                 ret = OK;
  780.                 goto kkk;
  781.             }
  782.         }
  783.         ret = 111;
  784.           delay();                    /* kludge delay */
  785.           Super(ostack);
  786.         goto formend;
  787.   }
  788. kkk:
  789.   disksiz = hdsiz;
  790.   delay();                    /* kludge delay */
  791.   if (v == OK)    {
  792.       v = format(dev, (UWORD)hinfo.hi_in);  /*format*/
  793.   }
  794. lll:
  795.   delay();                    /* kludge delay */
  796.   Super(ostack);
  797.   
  798.   if (v != 0)  {
  799.       ret = errcode(dev);
  800.       if (tsterr(ret) != OK)
  801.           formaterr(dev);
  802.       ret = ERROR;
  803.       goto formend;
  804.   }
  805.   
  806.   ret = OK;
  807.   rebootp = 1;
  808. formend:
  809.   erasemsg();    /* Erase formatting box */
  810.   if (ret == 111)        /* HDX may not support this drive */
  811.           ret = err(needinfo);
  812.   if (ret < 0) {
  813.       if (bsl > 0) free(bsl);
  814.       return ERROR;
  815.   }
  816.   
  817.   /*------------------------------------------*
  818.    * Markbad the device destructively.          *
  819.    * Bad Sectors found are added to the BSL.  *
  820.    * Write BSL to device.              *
  821.    *------------------------------------------*/
  822.   if (domark == YESMK)    { /* need to do the markbad */
  823.       if ((nbad = dsmarkbad(dev, hdsiz, 1, pattern)) < 0) {
  824.           free(bsl);
  825.           return ERROR;
  826.       }
  827.   }
  828.   if (wrbsl(dev) != OK) {
  829.       free(bsl);
  830.       return ERROR;
  831.   }
  832.   free(bsl);
  833.  
  834.     
  835.   /*
  836.    * Install boot-stopper in sector image;
  837.    * write root sector to device.
  838.    * 6-13-88  Setting of soft format parameters in root sector sets
  839.    *        the hard disk size only.
  840.    */
  841.   fillbuf(bs, 512L, 0L);    /* create new root sector */
  842.   sbslparm(bs);            /* set BSL parameters */
  843.   if (modesel)    {
  844.       sfmtparm(bs, &hinfo);
  845.   } else {
  846.       sdisksiz(bs, disksiz);
  847.   }
  848.   for (d = bs, s = &bootstop, cnt = (long)(&bootend - &bootstop); --cnt;)
  849.     *d++ = *s++;
  850.   Protobt(bs, -1L, -1, 1);    /* make root sector executable */
  851.   
  852.   if ((ret = putroot(dev, bs, (SECTOR)0)) != OK) {
  853.       if (tsterr(ret) != OK)
  854.           err(rootwrit);
  855.       return ERROR;
  856.   }
  857.  
  858.   /*
  859.    * Make a copy of the default partition name.
  860.    * Figure out the partition scheme id.
  861.    */
  862.   if (!noinfo)    {
  863.       s = wgetstr("pt");
  864.       strcpy(pnam, s);
  865.   }
  866.   /* ??
  867.   figprid(disksiz, pr_id);
  868.   */
  869.   dodipart(dev, pnam, disksiz);
  870.   return OK;
  871. }
  872.  
  873.  
  874.  
  875. /*
  876.  * Handle [PARTITION] item;
  877.  * if `xdev' is -1, throw up dialog boxes;
  878.  * if `xdev' >= 0, just partition the dev,
  879.  * using `pnam' as the partition type, 
  880.  * and `pr_id' to search for the type.
  881.  *
  882.  */
  883. dodipart(xdev, pnam, hsize)
  884. int xdev;
  885. char *pnam;
  886. long hsize;
  887. {
  888.     int dev, i, ret =OK, fine;
  889.     int choice;
  890.     char *seldev = "ACSI unit 0";
  891.       char *seldv =  "IDE  unit 0";
  892.     char *s;
  893.     char bs[512];
  894.     PART *pinfo;
  895.     int tem1, tem2;
  896.     long disksiz;
  897.     int mask = 0x0001;
  898.  
  899.     if (xdev < 0) {
  900.     /*
  901.      * Throw up warning saying that partition is dangerous;
  902.      * then get physical dev they want to clobber.
  903.      */
  904.     pwarning[PWARNCN].ob_state = NORMAL;
  905.     pwarning[PWARNOK].ob_state = NORMAL;
  906.     if (execform(pwarning) != PWARNOK) return BAILOUT;
  907.     tformat = FALSE;
  908.     if ((dev = gphysdev()) < 0) {
  909.         return BAILOUT;
  910.     }
  911.     /*
  912.      * Let the user edit/pick partitions.
  913.      */
  914.     fine = 0;
  915.     while (!fine) {
  916.         if (sfigpart(bs, dev, (PART *)&pinfo) != OK)    {
  917.             if (pinfo > 0)    Mfree(pinfo);
  918.             return BAILOUT;
  919.         }
  920.         if ((ret = chkpart(dev, pinfo)) != OK) {
  921.             if (ret < 0)    {    /* size too big */
  922.                 err(nexsmem);
  923.             } else {    /* some other errors  */
  924.                 if (pinfo > 0)    Mfree(pinfo);
  925.                 return BAILOUT;
  926.             }
  927.         } else {
  928.             fine = 1;
  929.         }
  930.     }
  931.  
  932.       fine = dev;            /* default for the acsi unit */
  933.     *seldev = 'A';
  934.       if (dev > 15)    { /* it is a IDE-AT drive */
  935.         fine = dev - 16;
  936.         seldev = seldv;
  937.     } else if (dev > 7)     {    /* minus 8 for the scsi drive number */
  938.         fine = dev - 8;
  939.         *seldev = 'S';
  940.     }
  941.     *(seldev + 10) = fine + '0';
  942.  
  943.     /* Last chance to bail out */
  944.     (partfnl[PUNIT].ob_spec)->te_ptext = seldev;
  945.     partfnl[PARTYES].ob_state = NORMAL;
  946.     partfnl[PARTNO].ob_state = NORMAL;
  947.     if (execform(partfnl) != PARTYES)    {
  948.         if (pinfo > 0)    Mfree(pinfo);
  949.         return BAILOUT;
  950.     }
  951.  
  952.     } else {
  953.         /* ??
  954.         if ((!noinfo) && (!ttscsi) && (wgetent(pnam, pr_id) != OK)) {
  955.             nopart[NOSCHPOK].ob_state = NORMAL;
  956.             (nopart[NOSCHPR].ob_spec)->te_ptext = pnam;
  957.             execform(nopart);
  958.             return ERROR;
  959.         }
  960.         */
  961.         npart = 4;
  962.         ext = NO_EXT;    /* set the extended partition flag to not exists */
  963.         dev = xdev;
  964.         if ((pinfo = (PART *)Malloc((long)sizeof(PART)*npart)) <= 0)    {
  965.             err(nomemory);
  966.             if (pinfo > 0)    Mfree(pinfo);
  967.             return ERROR;
  968.         }
  969.         inipart(pinfo, npart);
  970.         npart = 0;
  971.         setpart(pinfo, pnam, hsize);
  972.         /* ??
  973.         if (ttscsi)        {     SCSI bus drive 
  974.             setpart(pinfo, hsize);
  975.         } else {             regular drvie 
  976.             for (i = 0; i < 4; ++i)
  977.                 fillpart(i, &pinfo[i]);
  978.         }
  979.         */
  980.     }
  981.  
  982.     /* For REAL!! */
  983.     dsplymsg(partmsg);
  984.     
  985.     bsl = 0L;
  986.     
  987.     /* Get size of BSL */
  988.     if ((bslsiz = gbslsiz(dev)) > 0L) {
  989.         /* Allocate memory for existing BSL */
  990.         if ((bsl = (BYTE *)mymalloc((int)bslsiz*512)) <= 0) {
  991.             ret = err(nomemory);
  992.             goto partend;
  993.         }
  994.             
  995.         /* Read in BSL */
  996.         if ((ret = rdbsl(dev)) != OK) {
  997.             if (ret == INVALID)
  998.                 err(cruptbsl);
  999.             ret = ERROR;
  1000.             goto partend;
  1001.         }
  1002.     } else if (bslsiz == 0) {
  1003.         ret = err(oldfmt);
  1004.         goto partend;
  1005.     } else if (bslsiz == ERROR) {
  1006.         ret = err(rootread);
  1007.         goto partend;
  1008.     } else {
  1009.         ret = ERROR;
  1010.         goto partend;
  1011.     }
  1012.  
  1013.     
  1014.     /* Lay out partition headers */
  1015.     if (spheader(dev, &pinfo[0]) != OK) {
  1016.         ret = ERROR;
  1017.         goto partend;
  1018.     }
  1019.     
  1020.     if (wrbsl(dev) != OK) {        /* write BSL */
  1021.         ret = ERROR;
  1022.         goto partend;
  1023.     }
  1024.  
  1025.     /* check and change the structure's id after 'spheader()' */
  1026.     changeid(pinfo);
  1027.  
  1028.     /* Shove partition parms into root sector */
  1029.     if ((ret = getroot(dev, bs, (SECTOR)0)) != 0)    {
  1030.         if (tsterr(ret) != OK)
  1031.             err(rootread);
  1032.         ret = ERROR;
  1033.         goto partend;
  1034.     }
  1035.  
  1036.     if ((ret = stlayroot(bs, dev, pinfo)) != OK)    {
  1037.         goto partend;
  1038.     }
  1039.     tem1 = npart;            /* save the number of partitions */
  1040.     tem2 = ext;                /* save the index of extended partition */
  1041.     if (rescan(1,2)) {        /* has to be here because map changed    */
  1042.         ret = ERROR;        /* after partitions are moved around,    */
  1043.         goto partend;        /* report medium change error.        */
  1044.     }
  1045.     npart = tem1;
  1046.     ext = tem2;
  1047.     /* Partition the device with parameters given in pinfo */
  1048.     if (stlaybs(dev, &pinfo[0]) != OK)
  1049.         ret = ERROR;
  1050.     else
  1051.         ret = OK;
  1052.         
  1053.     rebootp = 1;
  1054.     /*  add on Jul 2, 90 for removable drive
  1055.     if ((typedev & (mask << dev)))             it is a removable drive 
  1056.         if (npart <= prevnpart)                 if less or equal than prevous 
  1057.             rebootp = 0;                      partition, don't reboot 
  1058.     change and add over on Jul 2, 90 for the removable drive on Oct 1, 90
  1059.     if ((typedev & (mask << dev)))    {         it is a removable drive 
  1060.         for (i = 0; i < 10; i++)     {
  1061.             if (!mediach(dev))            
  1062.                 break;
  1063.             if (i == 10)
  1064.                 rebootp = 1;
  1065.             else
  1066.                 rebootp = 0;
  1067.         }
  1068.     }
  1069.     */
  1070.     pnf = 1;        /* set the flag to just doing the partition */
  1071. partend:
  1072.     if (bsl > 0) free(bsl);
  1073.     inipart(pinfo, npart);
  1074.     if (pinfo > 0)    Mfree(pinfo);
  1075.     erasemsg();
  1076.     return (ret);
  1077. }
  1078.  
  1079.  
  1080. /*
  1081.  * get root sector informations and write them into that sector 
  1082.  */
  1083.  
  1084. stlayroot(bs, dev, part)
  1085. char *bs;
  1086. int dev;
  1087. PART *part;
  1088. {
  1089.     int i;
  1090.     UWORD sum = 0x1234;
  1091.     long cnt, disksiz, prvst;
  1092.     char *d, *s;
  1093.     extern char bootstop;
  1094.     extern char bootend;
  1095.  
  1096.     /* do the prime partition */
  1097.     spart(bs, part, 0, &prvst);    /* set ST partition parameters */
  1098.     /*
  1099.       sfmtparm(bs, &hinfo);
  1100.       for (d = bs, s = &bootstop, cnt = (long)(&bootend - &bootstop); --cnt;)
  1101.         *d++ = *s++;
  1102.     */
  1103.     sbslparm(bs);                /* set bsl parameters */
  1104.     Protobt(bs, -1L, -1, 1);        /* make root sector executable */
  1105.     if ((ret = putroot(dev, bs, (SECTOR)0)) != OK) {
  1106.         if (tsterr(ret) != OK)
  1107.             err(rootwrit);
  1108.         return(ERROR);
  1109.     }
  1110.     if (ext == NO_EXT)    return OK;        /* no extended partition */
  1111.     /* do the extended partitions */
  1112.     extsiz = part[ext].p_siz;
  1113.     for (i = 4; i < npart; i++)    {
  1114.         if (!(part[i].p_flg & P_EXISTS))    {     /* skip if not exists */
  1115.             return OK;
  1116.         }
  1117.         spart(bs, part, i, &prvst);    /* set IBM partition parameters */
  1118.         if ((ret = putroot(dev, bs, part[i].p_st)) != OK) {
  1119.             if (tsterr(ret) != OK)
  1120.                 err(rootwrit);
  1121.             return(ERROR);
  1122.         }
  1123.     }
  1124.     return OK;
  1125. }
  1126.  
  1127.  
  1128.  
  1129.  
  1130. /*
  1131.  * Put information into the root structure
  1132.  */
  1133.  
  1134. spart(image, pinfo, pnm, prvst)
  1135.  
  1136. char *image;            /* root sector buffer */
  1137. register PART *pinfo;    /* partition information */
  1138. int pnm;                /* partition number */
  1139. long *prvst;            /* The previous partition start sector */
  1140.  
  1141. {
  1142.     PART *rpart;
  1143.     long numcyl;
  1144.     int i, j;
  1145.  
  1146.     if (pnm)     {
  1147.         fillbuf(image, 512L, 0L);
  1148.     }
  1149.     rpart = &((RSECT *)(image + 0x200 - sizeof(RSECT)))->hd_p[0];
  1150.     if (pnm < 4)    {
  1151.         for (i = 0; i < NPARTS; i++, rpart++)    {
  1152.             if (pinfo->p_flg & P_EXISTS)    {
  1153.                 rpart->p_flg = P_EXISTS;
  1154.                 for (j = 0; j < 3; j++)
  1155.                     rpart->p_id[j] = pinfo->p_id[j];
  1156.                 rpart->p_st = pinfo->p_st;
  1157.                 rpart->p_siz = pinfo->p_siz;
  1158.             } else {
  1159.                 rpart->p_flg = 0;
  1160.                 for (j = 0; j < 3; j++)
  1161.                     rpart->p_id[j] = 0;
  1162.                 rpart->p_st = 0L;
  1163.                 rpart->p_siz = 0L;
  1164.             }
  1165.             pinfo++;
  1166.         }
  1167.  
  1168.     } else {    /* pnm => 4 */
  1169.         rpart->p_flg = pinfo[pnm].p_flg;
  1170.         for (j = 0; j < 3; j++)
  1171.             rpart->p_id[j] = pinfo[pnm].p_id[j];
  1172.         rpart->p_st = ROOTSECT;
  1173.         rpart->p_siz = pinfo[pnm].p_siz - ROOTSECT;
  1174.         rpart++;
  1175.         if (((pnm + 1) != npart) && (pinfo[pnm+1].p_flg & P_EXISTS)) { 
  1176.             /* need extened partition */
  1177.             rpart->p_flg = P_EXISTS;
  1178.             rpart->p_id[0] = 'X';
  1179.             rpart->p_id[1] = 'G';
  1180.             rpart->p_id[2] = 'M';
  1181.             rpart->p_siz = pinfo[pnm+1].p_siz;
  1182.             rpart->p_st = (pnm == 4) ? (pinfo[4].p_siz) :
  1183.                                     (pinfo[pnm].p_siz + *prvst);
  1184.             *prvst = rpart->p_st;
  1185.         }
  1186.  
  1187.     }
  1188. }
  1189.  
  1190.  
  1191.  
  1192. /*
  1193.  * Setup partitions on the disk;
  1194.  * write boot sectors and zero FATs and root directories.
  1195.  *
  1196.  */
  1197. stlaybs(physdev, pinfo)
  1198. int physdev;
  1199. register PART *pinfo;
  1200. {
  1201.     int i, ldev, ret;
  1202.     int kindfat;
  1203.     SECTOR data, nsect;
  1204.     char *devno="X";
  1205.     long ndirs;
  1206.     UWORD fatsiz;
  1207.     BOOT *bs;
  1208.     long serialno;
  1209.     extern long longrandom();
  1210.     extern long cell();
  1211.     char *buf, *buf1;
  1212.     long size, remsect;
  1213.     long datsect;
  1214.     long datclst; 
  1215.     long fatclst;
  1216.     long entsect;
  1217.  
  1218.     if ((bslsiz = gbslsiz(physdev)) < 0L) {
  1219.         if (bslsiz == ERROR)
  1220.             err(rootread);
  1221.         return ERROR;
  1222.     }
  1223.     /* SCAN_BS: pinfo is for scan() and laybs() use */
  1224.     if (ext != NO_EXT)    sortpart(pinfo, SCAN_BS);    
  1225.  
  1226.     for (i = 0; i < npart; ++i, ++pinfo) {
  1227.         
  1228.         /* don't care if partition does not exist */
  1229.         if (!(pinfo->p_flg & P_EXISTS)) {
  1230.             return OK;
  1231.         }
  1232.  
  1233.  
  1234.     /*
  1235.      * Compute boot sector parameters.
  1236.      */
  1237.         if (pinfo->p_siz > disksiz) {        /* partition >? disk size */
  1238.             *devno = i + '0';
  1239.             (part2big[BGPART].ob_spec)->te_ptext = devno;
  1240.             part2big[BGPARTOK].ob_state = NORMAL;
  1241.             execform(part2big);
  1242.             return ERROR;
  1243.         }
  1244.  
  1245.     /* estimat the bps */
  1246.     /* MAXSECT = 16MB - 8 */
  1247.     bps = cell((pinfo->p_siz-7)*BPS, (long)MAXSECT);
  1248.  
  1249.     /* the real bps */
  1250.     bps = BPS * n2power((UWORD)cell(bps, (long)BPS));
  1251.     ratio = bps / BPS;
  1252.     nsect = pinfo->p_siz / ratio;
  1253.  
  1254.     size = (long)BPS * ratio;
  1255.     if ((buf = (char *)Malloc(size)) <= 0)    {
  1256.         err(nomemory);
  1257.         if (buf > 0) Mfree((long)buf);
  1258.         return ERROR;
  1259.     }
  1260.  
  1261.     /*
  1262.      * Install entries in boot sector image;
  1263.      * force sector checksum to zero (non-executable);
  1264.      * write boot sector to media.
  1265.      *
  1266.       *    512 bytes/sector
  1267.      *    2 or 4 sectors/cluster (partition > 16Mb has 4 spc)
  1268.      *    1 reserved sector (for boot)
  1269.      *    2 FATs
  1270.      *    ... dir slots
  1271.      *    ... # sectors
  1272.      *    F8 means media is a hard disk
  1273.      *    ... FAT size
  1274.      *
  1275.      */
  1276.      
  1277.     /* Make a clean boot sector */
  1278.     fillbuf(buf, bps, 0L);
  1279.     bs = (BOOT *)buf;
  1280.         
  1281.  
  1282.     /* bytes per sector */
  1283.     iw((UWORD *)&bs->b_bps[0], (UWORD)bps);
  1284.     
  1285.     /* sectors per cluster */
  1286.     bs->b_spc = SPC;
  1287.         
  1288.     /* number of reserved sectors */
  1289.     iw((UWORD *)&bs->b_res[0], (UWORD)1);
  1290.     
  1291.     /* number of FATs */
  1292.     bs->b_nfats = 2;
  1293.     
  1294.  
  1295.     /*
  1296.      * Compute root directory size
  1297.      * 256 entries, plus fudge on devs > 10Mb
  1298.      */
  1299.     if (pinfo->p_siz < 0x5000L) ndirs = NUMEN;
  1300.     else ndirs = nsect / 80;    /* 1 dir entry per 80 sectors */
  1301.     /* round to nearest sector */
  1302.     ndirs = (ndirs + ((UWORD)bps/BPDIR - 1)) & ~((UWORD)bps/BPDIR - 1);    
  1303.     iw((UWORD *)&bs->b_ndirs[0], (UWORD)ndirs);
  1304.     
  1305.     /* number of sectors on media (partition) */
  1306.     iw((UWORD *)&bs->b_nsects[0], (UWORD)nsect);
  1307.  
  1308.     /* media descriptor */
  1309.     bs->b_media = 0xf8;
  1310.  
  1311.     /*--------------------------------------------------------------*
  1312.      * Compute FAT size                        *
  1313.      *                                *
  1314.      * Num entries to map the entire partition            *
  1315.      *    = partition's size in clusters                *
  1316.      *    = partition's size in sectors / spc            *
  1317.      *                                *
  1318.      * Num entries in FAT                        *
  1319.      *    = Num entries to map partition + reserved entries    *
  1320.      *    = (partition's size in sectors / spc) + 2        *
  1321.      *                                *
  1322.      * Num sectors FAT occupies                    *
  1323.      *    = Num entries in FAT / Num entries of FAT per sector    *
  1324.      *    = Num entries in FAT / (bps / 2)    <16-bit FAT>    *
  1325.      *    = ((partition's size in sectors / spc) + 2) / (bps/2) + 1    *
  1326.      *                        <+1 to round up>    *
  1327.      *--------------------------------------------------------------*/
  1328.     fatsiz = ((((nsect / bs->b_spc) + 2) * 2) / bps) + 1;/*bps ?? 512 or 1024*/
  1329.     iw((UWORD *)&bs->b_spf[0], (UWORD)fatsiz);
  1330.  
  1331.     /* write the serial number to the bs */ 
  1332.     Protobt(bs, 0x01000000, -1, -1);
  1333.  
  1334.     /*
  1335.      * Write partition's boot sector
  1336.      */
  1337.     forcesum((UWORD *)buf, (UWORD)0);    /* force image checksum */
  1338.     if ((ret = wrsects(physdev,(UWORD)ratio,buf,(SECTOR)pinfo->p_st))!= OK) {
  1339.         if (tsterr(ret) != OK)
  1340.             err(bootwrit);
  1341.         return ERROR;
  1342.     }
  1343.  
  1344.     /*
  1345.      * Zero the fat tables directory entry 
  1346.      */
  1347.     if ((ret = zerosect(physdev, (SECTOR)(pinfo->p_st+ratio),
  1348.              ((UWORD)((fatsiz*2 + ndirs*BPDIR/bps) * ratio)))) != OK) {
  1349.         if (tsterr(ret) != OK)
  1350.             err(hdrwrite);
  1351.         return ERROR;
  1352.     }
  1353.     /* Apr-23-93 jye: To fixed the same bug in the Gemdos.
  1354.      * Num entries in FAT                        *
  1355.      *    = Num entries to map partition + reserved entries    *
  1356.      *    = (partition's size in sectors / spc) + 2        *
  1357.      *                                *
  1358.      * Num sectors FAT occupies                    *
  1359.      *    = Num entries in FAT / Num entries of FAT per sector    *
  1360.      *    = Num entries in FAT / (bps / 2)    <16-bit FAT>    *
  1361.      * 
  1362.      * Num entries in the last sector of FAT
  1363.      * = (nsect/spc + 2) % (bps/2)
  1364.      * 
  1365.      * The pointer(or counter in byte)after the last entries in the FAT
  1366.      * = ((nsect/spc + 2) % (bps/2))*ratio*2 (16bit fat is 2 bytes)
  1367.      */
  1368.     /* # of sectors in datas:  
  1369.      *                 datsect   = nsect - boot - 2*fatsiz - size of dir 
  1370.      *                           = nsect-boot-2*fatsiz-(ndirs*32)/bps
  1371.      * # of clusters in datas: 
  1372.      *                datclst      = # of sectors in datas / SPC 
  1373.      * # of clusters(entries) in Fat: 
  1374.      *                fatclst   = fatsiz * (bps/2)
  1375.      * # of sectors need in fat talbe for datclst:
  1376.      *                entsect   = datclst / (bps/2)
  1377.      * # of sectors need to remodify to 0xffff:
  1378.      *                remsect   = fatsiz - entsect
  1379.      */
  1380.  
  1381.     datsect = nsect - 1 - 2*fatsiz - (ndirs*32)/bps;
  1382.     datclst = datsect / SPC;
  1383.     fatclst = fatsiz * (bps/2);
  1384.     entsect = datclst / (bps/2);
  1385.     remsect = (fatsiz - entsect)*size;
  1386.  
  1387.  
  1388.     /* # of sectors need to remodify to 0xffff */
  1389.     if ((buf1 = (char *)Malloc(remsect)) <= 0)    {
  1390.         err(nomemory);
  1391.         if (buf > 0) Mfree((long)buf);
  1392.         if (buf1 > 0) Mfree((long)buf1);
  1393.         return ERROR;
  1394.     }
  1395.  
  1396.     /* 
  1397.      * Apr-23-93 jye: To fixed the write over to next partitiona in the Gemdos  
  1398.      *                  , here just set those entries in Fat table, which don't
  1399.      *                  correspond to the availble date sectors in the partition.
  1400.      *                  The pointers should be:
  1401.      *                      p_st+(boot+datclst/(bps/2))*ratio and 
  1402.      *                    p_st+(boot+fatsiz+datclst/(bps/2))*ratio 
  1403.      */
  1404.     if ((ret = rdsects(physdev, (UWORD)(remsect/BPS), buf1, 
  1405.                      (SECTOR)pinfo->p_st+(1+datclst/(bps/2))*ratio)) != OK) {
  1406.         if (tsterr(ret) != OK)
  1407.             err(bootwrit);
  1408.         return ERROR;
  1409.     }
  1410.     /* fill the last sectors of FAT start at last entry with the 0xffff */
  1411.     /* +2 is for two reserved entries in the fat table */
  1412.     fillfat(buf1, ((datclst%(bps/2))+2)*2, remsect, 0xffff);
  1413.     if ((ret = wrsects(physdev,(UWORD)(remsect/BPS),buf1,
  1414.                      (SECTOR)pinfo->p_st+(1+datclst/(bps/2))*ratio) != OK) ||
  1415.         (ret = wrsects(physdev,(UWORD)(remsect/BPS),buf1,
  1416.              (SECTOR)pinfo->p_st+(1+fatsiz+datclst/(bps/2))*ratio) != OK)) {
  1417.         if (tsterr(ret) != OK)
  1418.             err(bootwrit);
  1419.         return ERROR;
  1420.     }
  1421.     /* Apr-23-93 jye: Add above codes to fix the Gemdos over write next
  1422.      *                   partition.
  1423.      */
  1424.  
  1425.              
  1426.     /*
  1427.      * Make first 2 entries in FATs more IBM-like.
  1428.      */
  1429.     if ((ret = rdsects(physdev,(UWORD)ratio,buf,
  1430.                         (SECTOR)(pinfo->p_st+ratio)))!= 0){
  1431.         if (tsterr(ret) != OK)
  1432.             err(fatread);
  1433.         return ERROR;
  1434.     }
  1435.     *(UWORD *)&buf[0] = 0xf8ff;
  1436.     *(UWORD *)&buf[2] = 0xffff;
  1437.     if ((ret = wrsects(physdev,(UWORD)ratio,
  1438.                         buf,(SECTOR)(pinfo->p_st+ratio)))!= 0 ||
  1439.         (ret = wrsects(physdev,(UWORD)ratio,buf,
  1440.                         (SECTOR)(pinfo->p_st+ratio+fatsiz*ratio)))
  1441.         != 0) {
  1442.         if (tsterr(ret) != OK)
  1443.             err(fatwrite);
  1444.         return ERROR;
  1445.     }
  1446.  
  1447.     /*
  1448.      * Mark bad sectors recorded in the BSL into the FATs.
  1449.      * Calculating parameters:
  1450.      *    ldev - from physdev and i.
  1451.      *    fat0 - always equals 1.
  1452.      *    fatsiz - calculated above.
  1453.      *    data - starts after the boot sector, 2 FATs and rootdir.
  1454.      */
  1455.  
  1456.         if (bslsiz > 0) {
  1457.             if ((ldev = phys2log(physdev, i)) == ERROR)
  1458.                 return parterr(physdev);
  1459.             data = (SECTOR)1 + (SECTOR)(fatsiz*2) + (SECTOR)(ndirs*BPDIR/bps);
  1460.             bsl2fat(ldev, (SECTOR)ratio, (UWORD)(fatsiz*ratio), 
  1461.                                                 data*ratio, MEDIA);
  1462.         }
  1463.         if (buf > 0) Mfree((long)buf);
  1464.         if (buf1 > 0) Mfree((long)buf1);
  1465.     }
  1466.     return OK;
  1467. }
  1468.  
  1469.  
  1470.  
  1471.  
  1472. /*
  1473.  * Handle [ZERO] item.
  1474.  *
  1475.  */
  1476. dodizero()
  1477. {
  1478.     int ldev, ret;
  1479.     char *seldev = "X";
  1480.     int i; 
  1481.  
  1482.     zwarning[ZWOK].ob_state = NORMAL;
  1483.     zwarning[ZWCN].ob_state = NORMAL;
  1484.     if (execform(zwarning) != ZWOK)
  1485.     return BAILOUT;
  1486.     if (showmany)    err(manyldev);
  1487.  
  1488.     if ((ldev = glogdev()) < 0) return BAILOUT;
  1489.  
  1490.     /* Find out if logical device has assumed parameters */
  1491.     if (chkparm(ldev) != OK) {
  1492.         wronparm[WRONOK].ob_state = NORMAL;
  1493.     execform(wronparm);
  1494.     return ERROR;
  1495.     }
  1496.         
  1497.     *seldev = ldev;
  1498.     (zerofnl[ZDRV].ob_spec)->te_ptext = seldev;
  1499.     strcat((zerofnl[ZDRV].ob_spec)->te_ptext, ":");
  1500.     zerofnl[ZYES].ob_state = NORMAL;
  1501.     zerofnl[ZNO].ob_state = NORMAL;
  1502.     if (execform(zerofnl) != ZYES)  return BAILOUT;
  1503.  
  1504.     dsplymsg(zeromsg);
  1505.     if (zero(ldev) == OK) {
  1506.     if (!rebootp) {
  1507.         for (i = 0; i < 10; i++) {
  1508.         if (!mediach(ldev-'A')) break;
  1509.         }
  1510.         if (i == 10) {
  1511.             rebootp = 1;
  1512.         err(mdach);
  1513.             }
  1514.     }
  1515.     }
  1516.     erasemsg();
  1517. }
  1518.  
  1519.  
  1520. /*
  1521.  * Handle [MARKBAD] item.
  1522.  *
  1523.  */
  1524. dodimark()
  1525. {
  1526.     int ldev, ret;
  1527.     int i;
  1528.  
  1529.     mwarning[MWARNOK].ob_state = NORMAL;
  1530.     mwarning[MWARNCN].ob_state = NORMAL;
  1531.     if (execform(mwarning) == MWARNCN)
  1532.         return BAILOUT;
  1533.     if (showmany)    err(manyldev);
  1534.  
  1535.     if ((ldev = glogdev()) < 0)
  1536.         return BAILOUT;
  1537.         
  1538.     /* Find out if logical device has assumed parameters */
  1539.     if (chkparm(ldev) != OK) {
  1540.         wronparm[WRONOK].ob_state = NORMAL;
  1541.     execform(wronparm);
  1542.     return ERROR;
  1543.     }
  1544.          
  1545.     dsplymsg(lmrkmsg);
  1546.     if (markbad(ldev) != OK) {
  1547.         erasemsg();
  1548.     } else {
  1549.         if (!rebootp) {
  1550.         for (i = 0; i < 10; i++) {
  1551.         if (!mediach(ldev-'A')) break;
  1552.         }
  1553.         if (i == 10) {
  1554.             rebootp = 1;
  1555.         err(mdach);
  1556.             }
  1557.         }
  1558.     }
  1559. }
  1560.  
  1561.  
  1562. /*
  1563.  * Translate unit number to tree index.
  1564.  *
  1565.  */
  1566. static int physxlat[] = {
  1567.     UNIT0, UNIT1, UNIT2, UNIT3
  1568. };
  1569.  
  1570. static int physid[] = {
  1571.     DRVID0, DRVID1, DRVID2, DRVID3
  1572. };
  1573.  
  1574. #define ROLL1 1            /* move the bar one step */
  1575. #define ROLL4 4            /* move the bar four steps */
  1576.  
  1577. /*
  1578.  * Ship selected devices.
  1579.  */
  1580. dodiship()
  1581. {
  1582.   
  1583.     int xrun = 1;
  1584.     int index=0, i, ret;
  1585.  
  1586.     linkdev();            /* link all live devices into a list */
  1587.     /* set form for first display */
  1588.     physdial[PHYSOK].ob_state = NORMAL;
  1589.     physdial[PHYSCN].ob_state = NORMAL;
  1590.     physdial[SLBOX].ob_y = 0;
  1591.     if (ndevs <= 4)    {
  1592.         physdial[SLBOX].ob_height = physdial[SLTRK].ob_height;
  1593.     } else {
  1594.         physdial[SLBOX].ob_height = physdial[SLTRK].ob_height / (ndevs/4.0);
  1595.     }
  1596.     ok2draw = 0;
  1597.     drawdev(index);    /* draw the devices into the dialog box */
  1598.     ARROW_MOUSE;
  1599.     dsplymsg(physdial);
  1600.     ++ok2draw;
  1601.     while (xrun)     {
  1602.         switch (form_do(physdial, -1))     {
  1603.             case PHYSOK:xrun = 0;
  1604.                         break;
  1605.             case PHYSCN:xrun = 0;            /* return */
  1606.                         break;
  1607.             case SLTRK: if (ndevs > 5)    {
  1608.                             scrupdn(ROLL4, &index);
  1609.                         }
  1610.                         break;
  1611.             case SLUP:  if (index != 0)    {
  1612.                             scrupdn(ROLL1, &index);
  1613.                         }
  1614.                         break;
  1615.             case SLDN:  if ((index < 12) && (index+4 < ndevs))    {
  1616.                             scrupdn(ROLL1, &index);
  1617.                         }
  1618.                         break;
  1619.             case SLBOX: if (ndevs > 5)    {
  1620.                             slidebox(physdial, &index);
  1621.                         }
  1622.                         break;
  1623.           }
  1624.       }
  1625.  
  1626.   /*
  1627.    * Draw shrinking box and cleanup the screen;
  1628.    * return thing that caused our exit.
  1629.    */
  1630.     erasemsg();
  1631.  
  1632.     if (ret == PHYSCN)    {
  1633.         return BAILOUT;
  1634.     }
  1635.     /* search for the selected unit */
  1636.     for (i=0; i < 4; i++)    {
  1637.         if (physdial[physxlat[i]].ob_state & SELECTED)    {
  1638.               /* Throw up final shipping warning. */
  1639.               shipfnl[SFNLCN].ob_state = NORMAL;
  1640.               shipfnl[SFNLOK].ob_state = NORMAL;
  1641.               if (execform(shipfnl) != SFNLOK) return BAILOUT;
  1642.             ship(devid[index+i].dev);
  1643.               /* Put out final message about turning off hard disks */
  1644.               scommand[TRNOFFOK].ob_state = NORMAL;
  1645.               execform(scommand);
  1646.         }
  1647.     }
  1648.     return BAILOUT;
  1649. }
  1650.   
  1651.  
  1652. /*
  1653.  * Get physical device#,
  1654.  * return devno or -1.
  1655.  *
  1656.  */
  1657. gphysdev()
  1658.  
  1659. {
  1660.     int xrun = 1, tpdev=0;
  1661.     int index=0, i, ret;
  1662.  
  1663.     linkdev();            /* link all live devices into a list */
  1664.     if (tformat)    {    /* when format, enable all devices in dialog box */
  1665.         if ((spscsixst)    || (atexst)) { /* sparrow or notebook */
  1666.             ndevs = 9;
  1667.             tpdev = 8;
  1668.         } else if (ttscsi) {
  1669.             ndevs = 16;
  1670.         } else { /* if ((!ttscsi) && (!noacsi))     Mega ST */
  1671.             ndevs = 8;
  1672.         }
  1673.     }
  1674.     /* set form for first display */
  1675.     physdial[PHYSOK].ob_state = NORMAL;
  1676.     physdial[PHYSCN].ob_state = NORMAL;
  1677.     physdial[SLBOX].ob_y = 0;
  1678.     if (ndevs <= 4)    {
  1679.         physdial[SLBOX].ob_height = physdial[SLTRK].ob_height;
  1680.     } else {
  1681.         physdial[SLBOX].ob_height = physdial[SLTRK].ob_height / (ndevs/4.0);
  1682.     }
  1683.     for (i=0; i < 4; i++)    {
  1684.         physdial[physxlat[i]].ob_state = NORMAL;
  1685.         physdial[physid[i]].ob_state = NORMAL;
  1686.     }
  1687.     ok2draw = 0;
  1688.     drawdev(index);    /* draw the devices into the dialog box */
  1689.     ARROW_MOUSE;
  1690.     dsplymsg(physdial);
  1691.     ++ok2draw;
  1692.     while (xrun)     {
  1693.         switch (ret=form_do(physdial, -1))     {
  1694.             case PHYSOK:xrun = 0;
  1695.                         break;
  1696.             case PHYSCN:xrun = 0;            /* return */
  1697.                         break;
  1698.             case SLTRK: if (ndevs > 5)    {
  1699.                             scrupdn(ROLL4, &index);
  1700.                         }
  1701.                         break;
  1702.             case SLUP:  if (index != 0)    {
  1703.                             scrupdn(ROLL1, &index);
  1704.                         }
  1705.                         break;
  1706.             case SLDN:  if ((index < 12) && (index+4 < ndevs))    {
  1707.                             scrupdn(ROLL1, &index);
  1708.                         }
  1709.                         break;
  1710.             case SLBOX: if (ndevs > 5)    {
  1711.                             slidebox(physdial, &index);
  1712.                         }
  1713.                         break;
  1714.             caseDRVID0:physdial[UNIT0].ob_state = SELECTED;
  1715.                        objc_draw(physdial,UNIT0, MAX_DEPTH, 0, 0, wdesk, hdesk);
  1716.                         break;;
  1717.             case DRVID1:physdial[UNIT1].ob_state = SELECTED;
  1718.                        objc_draw(physdial,UNIT1, MAX_DEPTH, 0, 0, wdesk, hdesk);
  1719.                         break;;
  1720.             case DRVID2:physdial[UNIT2].ob_state = SELECTED;
  1721.                        objc_draw(physdial,UNIT2, MAX_DEPTH, 0, 0, wdesk, hdesk);
  1722.                         break;;
  1723.             case DRVID3:physdial[UNIT3].ob_state = SELECTED;
  1724.                        objc_draw(physdial,UNIT3, MAX_DEPTH, 0, 0, wdesk, hdesk);
  1725.                         break;;
  1726.             case UNIT0:physdial[DRVID0].ob_state = SELECTED;
  1727.                        objc_draw(physdial,DRVID0, MAX_DEPTH, 0, 0, wdesk, hdesk);
  1728.                         break;;
  1729.             case UNIT1:physdial[DRVID1].ob_state = SELECTED;
  1730.                        objc_draw(physdial,DRVID1, MAX_DEPTH, 0, 0, wdesk, hdesk);
  1731.                         break;;
  1732.             case UNIT2:physdial[DRVID2].ob_state = SELECTED;
  1733.                        objc_draw(physdial,DRVID2, MAX_DEPTH, 0, 0, wdesk, hdesk);
  1734.                         break;;
  1735.             case UNIT3:physdial[DRVID3].ob_state = SELECTED;
  1736.                        objc_draw(physdial,DRVID3, MAX_DEPTH, 0, 0, wdesk, hdesk);
  1737.                         break;;
  1738.           }
  1739.       }
  1740.  
  1741.   /*
  1742.    * Draw shrinking box and cleanup the screen;
  1743.    * return thing that caused our exit.
  1744.    */
  1745.     erasemsg();
  1746.  
  1747.     if (ret == PHYSCN)    {
  1748.         return ERROR;
  1749.     }
  1750.     /* search for the selected unit */
  1751.     for (i=0; i < 4; i++)    {
  1752.         if (physdial[physxlat[i]].ob_state & SELECTED)    {
  1753.             if (tformat)    {
  1754.                 return(index+tpdev+i);
  1755.             } else {
  1756.                 return(devid[index+i].dev);
  1757.             }
  1758.         }
  1759.     }
  1760.     return ERROR;
  1761. }
  1762.  
  1763. /* draw the select devices dialog box for the format operation */
  1764.  
  1765. fdrawdev(index)
  1766. int index;
  1767. {
  1768.  
  1769.     int i, st=0;
  1770.  
  1771.     if ((spscsixst)    || (atexst))    {
  1772.         st = 8;
  1773.     }
  1774.     
  1775.     for (i=0; i < 4; i++)    {
  1776.         /*
  1777.         (physdial[physxlat[i]].ob_spec)->te_ptext = &drvid[36][0];
  1778.         (physdial[physid[i]].ob_spec)->te_ptext = &drvid[36][0];
  1779.         */
  1780.         if (i+index+st < 8)    {
  1781.               (physdial[physxlat[i]].ob_spec)->te_ptext = &drvid[19+i+index][0];
  1782.         } else if (i+index+st < 16)    {
  1783.         (physdial[physxlat[i]].ob_spec)->te_ptext = &drvid[27+i+index+st-8][0];
  1784.         } else {
  1785.             (physdial[physxlat[i]].ob_spec)->te_ptext = &drvid[35][0];
  1786.         }
  1787.         physdial[physxlat[i]].ob_state = NORMAL;
  1788.         physdial[physid[i]].ob_state = NORMAL;
  1789.         if (i+index+st == 16)    {    /* it is sparrow */
  1790.             (physdial[physid[i]].ob_spec)->te_ptext = &drvid[i+index+st][0];
  1791.         } else if (livedevs[i+index+st])    {
  1792.             if (idevs[i+index+st])    {
  1793.                 (physdial[physid[i]].ob_spec)->te_ptext = &drvid[i+index+st][0];
  1794.             } else {
  1795.                 (physdial[physid[i]].ob_spec)->te_ptext = &drvid[17][0];
  1796.             }
  1797.         } else {
  1798.             if (idevs[i+index+st])    {
  1799.                 (physdial[physid[i]].ob_spec)->te_ptext = &drvid[i+index+st][0];
  1800.             } else {
  1801.                 (physdial[physid[i]].ob_spec)->te_ptext = &drvid[18][0];
  1802.             }
  1803.             /*
  1804.             physdial[physid[i]].ob_state = NORMAL;
  1805.             */
  1806.         }
  1807.         if (ok2draw)    {
  1808.             /*
  1809.             objc_draw(physdial,physlat[i], MAX_DEPTH, 0, 0, wdesk, hdesk);
  1810.             */
  1811.             objc_draw(physdial,physxlat[i], MAX_DEPTH, 0, 0, wdesk, hdesk);
  1812.             objc_draw(physdial,physid[i], MAX_DEPTH, 0, 0, wdesk, hdesk);
  1813.         }
  1814.     }
  1815. }
  1816.     
  1817. /* draw the select devices dialog box for the partition operation */
  1818.  
  1819. drawdev(index)
  1820. int index;
  1821. {
  1822.  
  1823.     int i, j;
  1824.  
  1825.     if (tformat)    {
  1826.         fdrawdev(index);
  1827.         return OK;
  1828.     }
  1829.  
  1830.     for (i=0; i < 4; i++)    {
  1831.         (physdial[physxlat[i]].ob_spec)->te_ptext = &drvid[36][0];
  1832.         (physdial[physid[i]].ob_spec)->te_ptext = &drvid[36][0];
  1833.         if (devid[i+index].flg)    {
  1834.                physdial[physxlat[i]].ob_state = NORMAL;
  1835.                physdial[physid[i]].ob_state = NORMAL;
  1836.             /*
  1837.             if (devid[i+index].flg == 2)    {
  1838.                 physdial[physxlat[i]].ob_state = NORMAL;
  1839.             } else {
  1840.                 physdial[physxlat[i]].ob_state = NORMAL | OUTLINED;
  1841.             }
  1842.             */
  1843.             j = devid[i+index].dev;
  1844.             if (j < 8)    {
  1845.                 /*
  1846.                 drvid[19][10] = devid[i+index].dev + '0';
  1847.                 */
  1848.                 (physdial[physxlat[i]].ob_spec)->te_ptext = &drvid[19+j][0];
  1849.             } else if (j < 16)    {
  1850.                 /*
  1851.                 drvid[20][10] = devid[i+index].dev - 8 + '0';
  1852.                 */
  1853.                 (physdial[physxlat[i]].ob_spec)->te_ptext = &drvid[27+j-8][0];
  1854.             } else {
  1855.                 (physdial[physxlat[i]].ob_spec)->te_ptext = &drvid[35][0];
  1856.             }
  1857.             /*
  1858.             physdial[physid[i]].ob_state = NORMAL;
  1859.             */
  1860.             (physdial[physid[i]].ob_spec)->te_ptext = devid[i+index].id;
  1861.         } else {
  1862.             physdial[physxlat[i]].ob_state = DISABLED;
  1863.             physdial[physid[i]].ob_state = DISABLED;
  1864.             /*
  1865.             (physdial[physid[i]].ob_spec)->te_ptext = &drvid[18][0];
  1866.             */
  1867.         }
  1868.         if (ok2draw)    {
  1869.             objc_draw(physdial,physxlat[i], MAX_DEPTH, 0, 0, wdesk, hdesk);
  1870.             objc_draw(physdial,physid[i], MAX_DEPTH, 0, 0, wdesk, hdesk);
  1871.             /*
  1872.             objc_draw(physdial,physlat[i], MAX_DEPTH, 0, 0, wdesk, hdesk);
  1873.             */
  1874.         }
  1875.     }
  1876. }
  1877.  
  1878. scrupdn(roll, index)
  1879. int roll;
  1880. int *index;
  1881.  
  1882. {
  1883.      int gr_mkmx, gr_mkmy;
  1884.     int gr_mkmstate, gr_mkkstate;
  1885.     int barht, barx, bary;
  1886.     int eptx, epty, eptht;
  1887.  
  1888.     barht = physdial[SLTRK].ob_height;
  1889.     eptht = physdial[SLBOX].ob_height;
  1890.     graf_mkstate(&gr_mkmx, &gr_mkmy, &gr_mkmstate, &gr_mkkstate);
  1891.     objc_offset(physdial, SLBOX, &eptx, &epty);
  1892.     objc_offset(physdial, SLTRK, &barx, &bary);
  1893.     /* check which part of bar was clicked */
  1894.     if (gr_mkmy > (epty+eptht))    {    /* low part of bar was clicked */
  1895.         if (roll == ROLL4)    {
  1896.             /*
  1897.             if (eptht > (bary+barht-epty-eptht))    {
  1898.             if (((spscsixst)||(atexst)) && (*index+5 >= ndevs))    {
  1899.                 *index = ndevs - 4;
  1900.                 physdial[SLBOX].ob_y = barht - eptht;
  1901.             } else if (*index+5 >= ndevs)    {
  1902.             */
  1903.             if ((*index+5 >= ndevs) || (eptht > (bary+barht-epty-eptht)))    {
  1904.                 physdial[SLBOX].ob_y = barht - eptht;
  1905.                 *index = ndevs - 4;    /* the last 4 to display */
  1906.             } else if (*index+4 < ndevs)    {
  1907.                 /*
  1908.                 physdial[SLBOX].ob_y += barht / 4;
  1909.                 */
  1910.                 physdial[SLBOX].ob_y += eptht;
  1911.                 *index += 4;
  1912.             }
  1913.         } else {    /* ROLL1 */
  1914.             if ((*index+5 >= ndevs)||((barht/ndevs)>(bary+barht-epty-eptht))){
  1915.                 physdial[SLBOX].ob_y = barht - eptht;
  1916.                 *index = ndevs - 4;    /* the last 4 to display */
  1917.             } else {
  1918.                 physdial[SLBOX].ob_y += barht / ndevs;
  1919.                 *index += 1;
  1920.             }
  1921.         }
  1922.     } else if (gr_mkmy < epty)    {    /* upper part of bar was clicked */
  1923.         if (roll == ROLL4)    {
  1924.             if ((eptht > (epty - bary)) || (*index <= 4))    {
  1925.                 physdial[SLBOX].ob_y = 0;
  1926.                 *index = 0;
  1927.             } else {
  1928.                 /*
  1929.                 physdial[SLBOX].ob_y -= barht / 4;
  1930.                 */
  1931.                 physdial[SLBOX].ob_y -= eptht;
  1932.                 *index -= 4;
  1933.             }
  1934.         } else {
  1935.             if (*index <= 1)    {
  1936.                 physdial[SLBOX].ob_y = 0;
  1937.                 *index = 0;
  1938.             } else {
  1939.                 physdial[SLBOX].ob_y -= barht / ndevs;
  1940.                 *index -= 1;
  1941.             }
  1942.         }
  1943.     } else {
  1944.       return OK;
  1945.     }
  1946.     objc_draw(physdial,SLTRK, MAX_DEPTH, 0, 0, wdesk, hdesk);
  1947.     drawdev(*index);
  1948. }
  1949.  
  1950.  
  1951. slidebox(tree, index)
  1952.  
  1953. OBJECT *tree;
  1954. int *index;
  1955.  
  1956. {
  1957.     int gr_wreturn, eptatmax;
  1958.     int ind, tmp;
  1959.  
  1960.     ind = ndevs - 4;
  1961.     eptatmax = tree[SLTRK].ob_height - tree[SLBOX].ob_height;
  1962.     gr_wreturn = graf_slidebox(tree, SLTRK, SLBOX, 1);
  1963.     tmp = (ind * gr_wreturn) / 1000;
  1964.     if (tmp == *index)    {    /* slide box is in the same place */
  1965.         return OK;    
  1966.     } else {
  1967.         *index = tmp;
  1968.     }
  1969.     if (*index+4 > ndevs)    {
  1970.         *index = ndevs-4;
  1971.     } 
  1972.     tree[SLBOX].ob_y = (eptatmax * (*index)) / ind;
  1973.     objc_draw(tree, SLTRK, MAX_DEPTH, 0, 0, wdesk, hdesk);
  1974.     drawdev(*index);
  1975. }
  1976.  
  1977.  
  1978.  
  1979. /* put the live device into a list */
  1980.  
  1981. linkdev()
  1982. {
  1983.  
  1984.     int i, j=0;
  1985.  
  1986.     for (i=0; i<16; i++)    {
  1987.         devid[i].flg = 0;
  1988.         devid[i].id = &drvid[18][0];
  1989.         if (livedevs[i])    {
  1990.             devid[j].flg = P_EXISTS;
  1991.             devid[j].dev = i;
  1992.             if (idevs[i])    {
  1993.                 devid[j++].id = &drvid[i][0];
  1994.             } else {
  1995.                 devid[j++].id = &drvid[17][0];
  1996.             }
  1997.         } else if (idevs[i])    {
  1998.             devid[j].flg = 2;
  1999.             devid[j].dev = i;
  2000.             devid[j++].id = &drvid[i][0];
  2001.         }
  2002.     }
  2003.     if ((spscsixst) || (atexst))    {
  2004.         devid[j].flg = P_EXISTS;
  2005.         devid[j].dev = i;
  2006.         devid[j++].id = &drvid[i][0];
  2007.     }
  2008.  
  2009.     ndevs = j;    /* number of live devices */
  2010.  
  2011. }
  2012.  
  2013. ggphysdev()
  2014. {
  2015.     int i, j, endup, start, index=0;
  2016.     int ret, unit=8;
  2017.  
  2018.     /* Set up the dialog box for SCSI or ACSI or IDE-AT driver selection */
  2019. redogphy:
  2020.     typedial[SCSIYES].ob_state = NORMAL;
  2021.     typedial[ACSIYES].ob_state = NORMAL;
  2022.     typedial[IDEYES].ob_state = NORMAL;
  2023.     if (!tformat)    {
  2024.         /* check which type of unit was selected */
  2025.         if ((!ttscsi)&&(!spscsixst)&&(noacsi))    { /* don't need select */
  2026.             index = 16;
  2027.             unit = 2;
  2028.         } else if ((!ttscsi)&&(!spscsixst)&&(!atexst))    {/* don't need select */
  2029.             ;
  2030.         } else if ((!atexst) && (noacsi))    { /* don't need select */
  2031.             index = 8;
  2032.         } else {
  2033.             if ((!ttscsi)&&(!spscsixst))    {
  2034.                 typedial[SCSIYES].ob_state = DISABLED;
  2035.             } else if (!atexst)    {
  2036.                 typedial[IDEYES].ob_state = DISABLED;
  2037.             } else if (noacsi)    {
  2038.                 typedial[ACSIYES].ob_state = DISABLED;
  2039.             }
  2040.             if ((ret = execform(typedial)) == SCSIYES)    {
  2041.                 index = 8;
  2042.             } else if (ret == IDEYES)    {
  2043.                 index = 16;
  2044.                 unit = 2;
  2045.             }
  2046.         } 
  2047.     } else {     /* do format */
  2048.         if ((ret = execform(typedial)) == SCSIYES)    {
  2049.             index = 8;
  2050.         } else if (ret == IDEYES)    {
  2051.             index = 16;
  2052.             unit = 2;
  2053.         }
  2054.     }
  2055.     /*
  2056.      * Clean up and exec object;
  2057.      * shadow devs we KNOW are there.
  2058.      */
  2059.     physdial[PHYSOK].ob_state = NORMAL;
  2060.     physdial[PHYSCN].ob_state = NORMAL;
  2061.     
  2062.     /*
  2063.     if (tformat == TRUE) {
  2064.         start = 1;        start initializing at unit 0 
  2065.         physdial[physxlat[0]].ob_state = NORMAL;
  2066.     } else {
  2067.         start = 0;        start initializing at unit 1
  2068.     }
  2069.     */
  2070.     
  2071.     endup = index + unit;
  2072.     if (index == 16)    {
  2073.         physdial[physxlat[0]].ob_state = NORMAL;
  2074.         if ((spscsixst) || (atexst))    {
  2075.             (physdial[physid[0]].ob_spec)->te_ptext = &drvid[16][0];
  2076.         } else {
  2077.             (physdial[physid[0]].ob_spec)->te_ptext = &drvid[18][0];
  2078.         }
  2079.         for (i = 1; i < 8; i++)    {
  2080.             (physdial[physid[i]].ob_spec)->te_ptext = &drvid[18][0];
  2081.             physdial[physid[i]].ob_state = DISABLED;
  2082.             physdial[physxlat[i]].ob_state = DISABLED;
  2083.         }
  2084.     } else {
  2085.         for (i = 0; i < 8; i++)    {
  2086.             (physdial[physid[i]].ob_spec)->te_ptext = &drvid[18][0];
  2087.             if ((spscsixst)&&(tformat)&&(!index))    {
  2088.                 physdial[physid[i]].ob_state = DISABLED;
  2089.                 physdial[physxlat[i]].ob_state = DISABLED;
  2090.             } else if (tformat)    {    /* when format, all devices are on */
  2091.                 physdial[physxlat[i]].ob_state = NORMAL;
  2092.                 physdial[physid[i]].ob_state = NORMAL;
  2093.             } else {
  2094.                 physdial[physid[i]].ob_state = DISABLED;
  2095.                 physdial[physxlat[i]].ob_state = DISABLED;
  2096.             }
  2097.         }
  2098.     }
  2099.     /*
  2100.     if (ttscsi)             the hard drive is a SCSI drive 
  2101.         physdial[physxlat[8]].ob_state = NORMAL;
  2102.     */
  2103.     for (i = index, j = 0; i < endup; ++i, j++)        {
  2104.         if (livedevs[i])    {
  2105.             physdial[physxlat[j]].ob_state = NORMAL | SHADOWED;
  2106.             physdial[physid[j]].ob_state = NORMAL;
  2107.             if ((i < 16) && (idevs[i]))    {
  2108.                 (physdial[physid[j]].ob_spec)->te_ptext = &drvid[i][0];
  2109.             } else if ((i < 16) && (!index))    {
  2110.                 (physdial[physid[j]].ob_spec)->te_ptext = &drvid[17][0];
  2111.             }
  2112.         }
  2113.     }
  2114.  
  2115.  
  2116.     if (execform(physdial) != PHYSOK)
  2117.         return ERROR;
  2118.      
  2119.     /* search for the selected unit */
  2120.     for (i = 0; i < unit; ++i)
  2121.         if (physdial[physxlat[i]].ob_state & SELECTED)    {
  2122.             return(index+i);
  2123.             /*
  2124.             if (livedevs[index+i])
  2125.                 return(index+i);
  2126.             else    {    
  2127.                 form_alart(1, nodev);
  2128.                 goto redogphy;
  2129.             }
  2130.             */
  2131.         }
  2132.  
  2133.  
  2134.     return ERROR;            /* if no object selected */
  2135. }
  2136.  
  2137. /*
  2138.  * Translate from logical device number
  2139.  * to object number in logical device
  2140.  * dialouge box.
  2141.  */
  2142. int logxlat[] = {
  2143.     CCOLON, DCOLON, ECOLON, FCOLON,
  2144.     GCOLON, HCOLON, ICOLON, JCOLON,
  2145.     KCOLON, LCOLON, MCOLON, NCOLON,
  2146.     OCOLON, PCOLON
  2147. };
  2148.  
  2149.  
  2150. /*
  2151.  * Get logical device,
  2152.  * return 'C'...'P'
  2153.  * or -1.
  2154.  *
  2155.  */
  2156. glogdev()
  2157. {
  2158.     int i, flg;
  2159.  
  2160.     /*
  2161.      * Setup tree; selectively enable drive buttons
  2162.      * and so on.
  2163.      */
  2164.     logdial[LOGOK].ob_state = NORMAL;
  2165.     logdial[LOGCN].ob_state = NORMAL;
  2166.     for (i = 0; i < MAXLOGDEVS; ++i) {
  2167.     if (logmap[i].lm_physdev < 0)
  2168.         flg = DISABLED;
  2169.         else flg = NORMAL;
  2170.     logdial[logxlat[i]].ob_state = flg;
  2171.     }
  2172.  
  2173.     if (execform(logdial) != LOGOK) return -1;
  2174.  
  2175.     for (i = 0; i < MAXLOGDEVS; ++i)
  2176.     if (logdial[logxlat[i]].ob_state & SELECTED)
  2177.         return i + 'C';
  2178.  
  2179.     return -1;
  2180. }
  2181.  
  2182.  
  2183. /*
  2184.  * Open virtual workstation.
  2185.  *
  2186.  */
  2187. open_vwork()
  2188. {
  2189.     int i;
  2190.  
  2191.     for (i = 0; i < 10;)
  2192.     work_in[i++] = 1;
  2193.     work_in[10] = 2;
  2194.     handle = phys_handle;
  2195.     v_opnvwk(work_in, &handle, work_out);
  2196. }
  2197.  
  2198.  
  2199. /*
  2200.  * Find and redraw all clipping rectangles
  2201.  *
  2202. */
  2203. Do_redraw(xc, yc, wc, hc)
  2204. int xc, yc, wc, hc;
  2205. {
  2206.     GRECT t1, t2;
  2207.     int temp[4];
  2208.  
  2209.     hide_mouse();
  2210.     t2.g_x=xc;
  2211.     t2.g_y=yc;
  2212.     t2.g_w=wc;
  2213.     t2.g_h=hc;
  2214.     vsf_interior(handle, 1);
  2215.     vsf_color(handle, 0);
  2216.     wind_get(wi_handle, WF_FIRSTXYWH, &t1.g_x, &t1.g_y, &t1.g_w, &t1.g_h);
  2217.     while (t1.g_w && t1.g_h) {
  2218.     if (rc_intersect(&t2, &t1)) {
  2219.         set_clip(t1.g_x, t1.g_y, t1.g_w, t1.g_h);
  2220.         temp[0] = xwork;
  2221.         temp[1] = ywork;
  2222.         temp[2] = xwork + wwork - 1;
  2223.         temp[3] = ywork + hwork - 1;
  2224.         v_bar(handle, temp);
  2225.     }
  2226.     wind_get(wi_handle, WF_NEXTXYWH, &t1.g_x, &t1.g_y, &t1.g_w, &t1.g_h);
  2227.     }
  2228.  
  2229.     show_mouse();
  2230. }
  2231.  
  2232.  
  2233. /*
  2234.  * Hide the mouse.
  2235.  *
  2236.  */
  2237. hide_mouse()
  2238. {
  2239.     if (!hidden) {
  2240.     graf_mouse(M_OFF, 0L);
  2241.     hidden = TRUE;
  2242.     }
  2243. }
  2244.  
  2245.  
  2246. /*
  2247.  * Show the mouse.
  2248.  *
  2249.  */
  2250. show_mouse() 
  2251. {
  2252.     if (hidden) {
  2253.     graf_mouse(M_ON, 0L);
  2254.     hidden = FALSE;
  2255.     }
  2256. }
  2257.  
  2258.  
  2259. /*
  2260.  * Set clipping rectangle.
  2261.  *
  2262.  */
  2263. set_clip(x, y, w, h)
  2264. int x, y, w, h;
  2265. {
  2266.     int clip[4];
  2267.  
  2268.     clip[0] = x;
  2269.     clip[1] = y;
  2270.     clip[2] = x + w;
  2271.     clip[3] = y + h;
  2272.     vs_clip(handle, 1, clip);
  2273. }
  2274.  
  2275.  
  2276. /*
  2277.  * "Execute" form,
  2278.  * return thingy that caused the exit.
  2279.  *
  2280.  */
  2281. execform(tree)
  2282. OBJECT tree[];
  2283. {
  2284.     int thingy;
  2285.  
  2286.     ARROW_MOUSE;
  2287.     dsplymsg(tree);
  2288.     thingy = form_do(tree, 0);
  2289.     erasemsg();
  2290.     BEE_MOUSE;
  2291.     return thingy;
  2292. }
  2293.  
  2294.  
  2295. /*
  2296.  *  Display a dialogue box on the screen.
  2297.  *    Input:
  2298.  *        tree - object tree for dialogue box to be displayed.
  2299.  *    Output:
  2300.  *        formw, formh, sx, sy, lx, ly - dimensions of box.
  2301.  */
  2302. dsplymsg(tree)
  2303. OBJECT *tree;
  2304. {
  2305.     form_center(tree,&lx, &ly, &formw, &formh);
  2306.  
  2307.     /*
  2308.     sx = lx + formw / 2;
  2309.     sy = ly + formh / 2;
  2310.     */
  2311.     
  2312.     form_dial(1, 0, 0, 0, 0, lx, ly, formw, formh);
  2313.     objc_draw(tree, 0, MAX_DEPTH, 0, 0, wdesk, hdesk);
  2314. }
  2315.  
  2316.  
  2317. /*
  2318.  *  Erase a dialogue box from the screen.
  2319.  *    Input:
  2320.  *        formw, formh, sx, sy, lx, ly - dimensions of box.
  2321.  */
  2322. erasemsg()
  2323. {
  2324.     form_dial(3, 0, 0, 0, 0, lx, ly, formw, fOrmh);
  2325. }
  2326.  
  2327.  
  2328.  
  2329. /*
  2330.  *  Make a long (4-byte) random.
  2331.  */ 
  2332. long
  2333. longrandom()
  2334. {
  2335.     long pattern;
  2336.     
  2337.     pattern = Random();
  2338.     pattern <<= 16;
  2339.     pattern ^= Random();
  2340.     
  2341.     return (pattern);
  2342. }
  2343.  
  2344. changeid(part)
  2345. PART *part;
  2346. {
  2347.     int i;
  2348.     long psiz;
  2349.  
  2350.     for(i = 0; i < npart; i++)    {
  2351.         if (i == ext)    continue;
  2352.         if (!(part[i].p_flg & P_EXISTS)) return OK;
  2353.         if (i > 3)    {
  2354.             psiz = part[i].p_siz - ROOTSECT;
  2355.         } else {
  2356.             psiz = part[i].p_siz;
  2357.         }
  2358.         if (psiz < MB16)    {
  2359.             part[i].p_id[0] = 'G';
  2360.             part[i].p_id[1] = 'E';
  2361.             part[i].p_id[2] = 'M';
  2362.         } else {
  2363.             part[i].p_id[0] = 'B';
  2364.             part[i].p_id[1] = 'G';
  2365.             part[i].p_id[2] = 'M';
  2366.         }
  2367.     }
  2368. }
  2369.  
  2370.